home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 187_01 / zfill.c < prev   
C/C++ Source or Header  |  1985-12-29  |  1KB  |  42 lines

  1. /*@*****************************************************/
  2. /*@                                                    */
  3. /*@ z_fill - zero fill a string in a field of size len */
  4. /*@        The string will be lengthened, if necessary.*/
  5. /*@                                                    */
  6. /*@   Usage:   z_fill(str, len);                       */
  7. /*@       where str is the string area.                */
  8. /*@             len is the field size.                 */
  9. /*@                                                    */
  10. /*@   Returns a pointer to the input string.           */
  11. /*@                                                    */
  12. /*@   NOTE:  str must be at least len+1 chars long.    */
  13. /*@                                                    */
  14. /*@*****************************************************/
  15.  
  16.  
  17. char *z_fill(str, size)
  18. char *str;
  19. int size;
  20. {
  21.     char *s, *d;
  22.     int len, count;
  23.  
  24.     len = strlen(str);                /* get string length */
  25.  
  26.     if (len > size)                    /* truncate, if necessary */
  27.         str[size] = 0x00;
  28.     else if (len < size) {
  29.         d = str + size;                /* copy to leave room */
  30.         s = str + len;
  31.         count = len + 1;
  32.         while (count--)
  33.             *d-- = *s--;
  34.         count = size - len;            /* number of zeros to insert */
  35.         s = str;
  36.         while (count--)
  37.             *s++ = '0';                /* add leading zeros */
  38.     }
  39.  
  40.     return str;
  41. }
  42.